package com.gome.ocean.server.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gome.ocean.dao.cango.CangoInstancesDAO;
import com.gome.ocean.dao.model.cango.CangoInstances;
import com.gome.ocean.server.service.ICangoInstancesService;

/**
 * @author zhangguoqing 2017年2月15日 下午5:20:04
 * @since 5.0.0
 */
@Service("iCangoInstancesService")
public class CangoInstancesServiceImpl implements ICangoInstancesService {

    private Logger LOG = LoggerFactory.getLogger(CangoInstancesServiceImpl.class);

    @Autowired
    private CangoInstancesDAO cangoInstanceDao;

    @Override
    public void batchUpdateState(List<CangoInstances> instanceLists) {
        if (instanceLists != null && instanceLists.size() > 0) {
            for (CangoInstances instance : instanceLists) {
                cangoInstanceDao.update(instance);
            }
        }

    }

    @Override
    public void saveOrUpdate(CangoInstances instance) {
        CangoInstances cangoInstance = cangoInstanceDao.findByName(instance.getName());
        if (cangoInstance != null) {
            cangoInstanceDao.update(instance);
        } else {
            cangoInstanceDao.insert(instance);
        }
    }

    @Override
    public boolean insert(CangoInstances instances) {
        boolean flag = false;
        try {
            cangoInstanceDao.insert(instances);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean update(CangoInstances instances) {
        boolean flag = false;
        try {
            cangoInstanceDao.update(instances);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean deleteById(Long id) {
        boolean flag = false;
        try {
            cangoInstanceDao.deleteById(id);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public CangoInstances findByName(String instanceName) {
        return cangoInstanceDao.findByName(instanceName);
    }

    @Override
    public boolean deleteAll() {
        boolean flag = false;
        try {
            cangoInstanceDao.deleteAll();
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public List<CangoInstances> findByCondition(Map<String, Object> map) {
        try {
            List<CangoInstances> list = cangoInstanceDao.findByCondition(map);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean deleteByInstanceName(String instanceName) {
        try {
            cangoInstanceDao.deleteByInstanceName(instanceName);
            return true;
        } catch (Exception e) {
            LOG.error(ExceptionUtils.getMessage(e));
        }
        return false;
    }

}
